{% if parent_page is undefined %}
  {% set parent_page = currentpage %}
{% endif %}
{% if depth is undefined %}
  {% set depth = 5 %}
{% endif %}
{% if count is undefined %}
  {# count controls how many direct children to print at each level #}
  {% set count = 9999 %}
{% endif %}

{% if show_blurbs is undefined %}
  {% set show_blurbs = False %}
{% endif %}

{% if parent_page.funnel is undefined %}
  {% set parent_level = "home" %}
  {% set level_2 = "funnel" %}
  {% set childpages = pages %}{# special case for home #}
{% elif parent_page == pages|selectattr("funnel", 'defined_and_equalto', parent_page.funnel)|first %}
  {% set parent_level = "funnel" %}
  {% set level_2 = "doc_type" %}
{% elif parent_page.doc_type is defined and parent_page == pages|selectattr("doc_type", 'defined_and_equalto', parent_page.doc_type)|first %}
  {% set parent_level = "doc_type" %}
  {% set level_2 = "supercategory" %}
{% elif parent_page.supercategory is defined and parent_page == pages|selectattr("supercategory", 'defined_and_equalto', parent_page.supercategory)|first %}
  {% set parent_level = "supercategory" %}
  {% set level_2 = "category" %}
{% elif parent_page.category is defined and parent_page == pages|selectattr("category", 'defined_and_equalto', parent_page.category)|first %}
  {% set parent_level = "category" %}
  {% set level_2 = "subcategory" %}
{% elif parent_page.subcategory is defined and parent_page == pages|selectattr("subcategory", 'defined_and_equalto', parent_page.subcategory)|first %}
  {% set parent_level = "subcategory" %}
  {% set level_2 = None %}
{% else %}
  <!-- TEMPLATE ERROR: page {{parent_page.name}} is not the parent of anything? -->
{% endif %}
{% if parent_level != "home" %}
  {% set childpages = pages|selectattr(parent_level, 'defined_and_equalto', parent_page[parent_level])|list %}
{% endif %}

{% macro print_children(parent, thosepages, indent_level, next_level_field, depth_limit, count_limit) %}
  {% set printed_next_levels = [] %}
  {% set ns=namespace(count_printed = 0) %}
  {% for onepage in thosepages %}
    {% if onepage == parent %}{# pass #}
    {% elif onepage.template == "template-redirect.html" %}{# don't list redirects #}
    {% elif next_level_field == None or (onepage[next_level_field] is undefined and next_level_field != "supercategory") %}
      {# direct child, print it! #}
      {% if ns.count_printed < count_limit %}
        <li class="level-{{indent_level}}"><a href="{{onepage.html}}">{{onepage.name}}</a>{% if show_blurbs and onepage.blurb is defined and indent_level == 1%}<p class="blurb child-blurb">{{onepage.blurb}}</p>{% endif %}</li>
        {% set ns.count_printed = ns.count_printed + 1 %}
      {% endif %}

    {% elif onepage[next_level_field] is undefined and next_level_field == "supercategory" %}
      {# special case, might need to skip "supercategory" #}

      {% if onepage.category is defined %}
        {# possible direct child or grandchild due to skipped level #}

        {% if onepage.category not in printed_next_levels %}
          {% if ns.count_printed < count_limit %}
            {% set next_level_children = thosepages|selectattr("category",
                'defined_and_equalto', onepage.category)|list %}
            {% set next_level_parent = next_level_children|first %}
            <li class="level-{{indent_level}}"><a href="{{next_level_parent.html}}">{{next_level_parent.name}}</a>{% if show_blurbs and next_level_parent.blurb is defined and indent_level == 1 %}<p class="blurb child-blurb">{{next_level_parent.blurb}}</p>{% endif %}</li>
            {% if indent_level+1 <= depth_limit and next_level_field != None %}
              {# Recurse! #}
              {% set field_after = "subcategory" %}
              {{ print_children(next_level_parent, next_level_children, indent_level+1, field_after, depth_limit, count_limit) }}
            {% endif %}
            {% set _ = printed_next_levels.append(onepage.category) %}
            {% set ns.count_printed = ns.count_printed + 1 %}
          {% endif %}
        {% endif %}

      {% else %}
        {% if ns.count_printed < count_limit %}
          <!-- Template WARNING: no category for page '{{onepage.name}}'? -->
          <li class="level-{{indent_level}}"><a href="{{onepage.html}}">{{onepage.html}}</a>{% if show_blurbs and onepage.blurb is defined and indent_level == 1 %}<p class="blurb child-blurb">{{onepage.blurb}}</p>{% endif %}</li>
          {% set ns.count_printed = ns.count_printed + 1 %}
        {% endif %}
      {% endif %}

    {% elif onepage[next_level_field] is defined %}
      {# might be a grandchild or lower #}
      {% if onepage[next_level_field] not in printed_next_levels %}
        {% if ns.count_printed < count_limit %}
          {% set next_level_children = thosepages|selectattr(next_level_field,'defined')|selectattr(next_level_field,
              'defined_and_equalto', onepage[next_level_field])|list %}
          {% set next_level_parent = next_level_children|first %}
          <li class="level-{{indent_level}}"><a href="{{next_level_parent.html}}">{{next_level_parent.name}}</a>{% if show_blurbs and next_level_parent.blurb is defined and indent_level == 1 %}<p class="blurb child-blurb">{{next_level_parent.blurb}}</p>{% endif %}</li>
          {% if (indent_level+1 <= depth_limit and next_level_field != None)
            or (next_level_field == "supercategory" and depth_limit == 1.5) %}
            {# Recurse! #}

            {# figure out what level to use next #}
            {% if next_level_field == "subcategory" %}
              {% set field_after = None %}
            {% elif next_level_field == "category" %}
              {% set field_after = "subcategory" %}
            {% elif next_level_field == "supercategory" %}
              {% set field_after = "category" %}
            {% elif next_level_field == "doc_type" %}
              {% set field_after = "supercategory" %}
            {% elif next_level_field == "funnel" %}
              {% set field_after = "doc_type" %}
            {% else %}
              <!-- ERROR: child-pages template reached unexpected next_level_field case. -->
            {% endif %}

            {{ print_children(next_level_parent, next_level_children, indent_level+1, field_after, depth_limit, count_limit) }}
          {% endif %}
          {% set _ = printed_next_levels.append(onepage[next_level_field]) %}
          {% set ns.count_printed = ns.count_printed + 1 %}
        {% endif %}
      {% endif %}
    {% endif %}
  {% endfor %}

  {# Special case to add XRPL Explorer to Dev Tools #}
  {% if parent.name == "Dev Tools" %}
  <li class="level-{{indent_level}}"><a target="_blank" href="https://livenet.xrpl.org/">XRP Ledger Explorer</a> <i class="fa fa-external-link"></i></li>
  {% endif %}
{% endmacro %}

<div class="children-display">
  <ul>
    {{ print_children(parent_page, childpages, 1, level_2, depth, count) }}
  </ul>
</div>
